home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / MULTISET.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  1KB  |  54 lines

  1. #include <set>
  2.  
  3.  using namespace std;
  4.  
  5.  typedef multiset<int,less<int> > set_type;
  6.  
  7.  ostream& operator<< (ostream& out, const set_type& s)
  8.  {
  9.    copy(s.begin(),s.end(),ostream_iterator<set_type::value_type>(cout," "));
  10.    return out;
  11.  }
  12.  
  13.  int main ()
  14.  {
  15.    //
  16.    // Create a multiset of integers.
  17.    //
  18.    set_type  si;
  19.    int       i;
  20.  
  21.    for (int j = 0; j < 2; j++)
  22.    {
  23.      for (i = 0; i < 10; ++i)
  24.        //
  25.        // Insert values with a hint.
  26.        //
  27.        si.insert(si.begin(), i);
  28.    }
  29.    //
  30.    // Print out the multiset.
  31.    //
  32.    cout << si << endl;
  33.    //
  34.    // Make another int multiset and an empty multiset.
  35.    //
  36.    set_type si2, siResult;
  37.    for (i = 0; i < 10; i++)
  38.       si2.insert(i+5);
  39.    cout << si2 << endl;
  40.    //
  41.    // Try a couple of set algorithms.
  42.    //
  43.    set_union(si.begin(),si.end(),si2.begin(),si2.end(),
  44.              inserter(siResult,siResult.begin()));
  45.    cout << "Union:" << endl << siResult << endl;
  46.  
  47.    siResult.erase(siResult.begin(),siResult.end());
  48.    set_intersection(si.begin(),si.end(),si2.begin(),si2.end(),
  49.                     inserter(siResult,siResult.begin()));
  50.    cout << "Intersection:" << endl << siResult << endl;
  51.  
  52.    return 0;
  53.  }
  54.